home *** CD-ROM | disk | FTP | other *** search
/ 3D Game Programming All in One / 3D Game Programming All in One Disc.iso / 3D2E / demo / server / scripts / radiusDamage.cs < prev    next >
Text File  |  2005-11-23  |  2KB  |  55 lines

  1. //-----------------------------------------------------------------------------
  2. // Torque Game Engine 
  3. // Copyright (C) GarageGames.com, Inc.
  4. //-----------------------------------------------------------------------------
  5.  
  6. // Support function which applies damage to objects within the radius of
  7. // some effect, usually an explosion.  This function will also optionally 
  8. // apply an impulse to each object.
  9.  
  10. function radiusDamage(%sourceObject, %position, %radius, %damage, %damageType, %impulse)
  11. {
  12.    // Use the container system to iterate through all the objects
  13.    // within our explosion radius.  We'll apply damage to all ShapeBase
  14.    // objects.
  15.    InitContainerRadiusSearch(%position, %radius, $TypeMasks::ShapeBaseObjectType);
  16.  
  17.    %halfRadius = %radius / 2;
  18.    while ((%targetObject = containerSearchNext()) != 0) {
  19.  
  20.       // Calculate how much exposure the current object has to
  21.       // the explosive force.  The object types listed are objects
  22.       // that will block an explosion.  If the object is totally blocked,
  23.       // then no damage is applied.
  24.       %coverage = calcExplosionCoverage(%position, %targetObject,
  25.          $TypeMasks::InteriorObjectType |  $TypeMasks::TerrainObjectType |
  26.          $TypeMasks::ForceFieldObjectType | $TypeMasks::VehicleObjectType);
  27.       if (%coverage == 0)
  28.          continue;
  29.  
  30.       // Radius distance subtracts out the length of smallest bounding
  31.       // box axis to return an appriximate distance to the edge of the
  32.       // object's bounds, as opposed to the distance to it's center.
  33.       %dist = containerSearchCurrRadiusDist();
  34.  
  35.       // Calculate a distance scale for the damage and the impulse.
  36.       // Full damage is applied to anything less than half the radius away,
  37.       // linear scale from there.
  38.       %distScale = (%dist < %halfRadius)? 1.0:
  39.          1.0 - ((%dist - %halfRadius) / %halfRadius);
  40.  
  41.       // Apply the damage
  42.       %targetObject.damage(%sourceObject, %position,
  43.          %damage * %coverage * %distScale, %damageType);
  44.  
  45.       // Apply the impulse
  46.       if (%impulse) 
  47.       {
  48.          %impulseVec = VectorSub(%targetObject.getWorldBoxCenter(), %position);
  49.          %impulseVec = VectorNormalize(%impulseVec);
  50.          %impulseVec = VectorScale(%impulseVec, %impulse * %distScale);
  51.          %targetObject.applyImpulse(%position, %impulseVec);
  52.       }
  53.    }
  54. }
  55.